﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using IDAL;
using Model;
using log4net;
using log4net.Config;

namespace DAL
{
    public class TProductType_PIMInfo : BaseRepository<TProductType_PIMInfoInfo>, ITProductType_PIMInfo
    {
        private const string PARM_TPRODUCTTYPE_PIM_PRODUCTTYPEID = "@ProductTypeID";
        private const string PARM_TPRODUCTTYPE_PIM_TESTITEMNAME = "@TestItemName";
        private const string PARM_TPRODUCTTYPE_PIM_TESTFRE = "@TestFre";
        private const string PARM_TPRODUCTTYPE_PIM_TESTPORT = "@TestPort";
        private const string PARM_TPRODUCTTYPE_PIM_TESTANGLE = "@TestAngle";

        private const string PARM_TPRODUCTTYPE_PIM_PRODUCTSN = "@ProductSN";
        
        private static readonly ILog log = log4net.LogManager.GetLogger("MyLogger");
        
        public IList<TProductType_PIMInfoInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("TProductType_PIMInfo_select", null);
        }

        /// <summary>
        /// Get an individual category based on a provided id
        /// </summary>
        /// <param name="categoryId">Category id</param>
        /// <returns>Details about the Category</returns>
        public TProductType_PIMInfoInfo LoadEntity<T1>(T1 item)
        {
            TPPIMItem tppi = (item is TPPIMItem) ? (item as TPPIMItem) : (TPPIMItem)null;
            SqlParameter[] Parms = GetItemParameters();
            Parms[0].Value = tppi.ProductTypeID;
            Parms[1].Value = tppi.TestItemName;
            return ExcuteSqlToGetEntity("TProductType_PIMInfo_selectbyIDTI", Parms);
        }

        public bool DeleteItem(TPPIMItem item)
        {
            SqlParameter[] Parms = GetItemParameters();
            Parms[0].Value = item.ProductTypeID;
            Parms[1].Value = item.TestItemName;
            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            try
            {
                ExecuteCommand(cmd, "TProductType_PIMInfo_Delete");
            }
            catch
            {
                if (log.IsDebugEnabled) log.Debug("天线产品类型交调配置项插入出错!"); 
                return false;
            }
            return true;
        }

        public IList<TProductType_PIMInfoInfo> GetTProductType_PIMInfobyID(string productTypeID)
        {
            SqlParameter parm = GetParameter();
            parm.Value = productTypeID;
            return ExcuteSqlToGetEntities("TProductType_PIMInfo_selectbyID", parm);
        }

        public IList<TProductType_PIMInfoInfo> GetTProductType_PIMInfobySN(string productSN)
        {
            SqlParameter parm = new SqlParameter(PARM_TPRODUCTTYPE_PIM_PRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("TProductType_PIMInfo_selectbySN", parm);
        }

        #region 重写超类基础操作

        public override TProductType_PIMInfoInfo GetEntityRecord(SqlDataReader rdr)
        {
            TProductType_PIMInfoInfo pro = new TProductType_PIMInfoInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetValue(2).ToString(), rdr.GetValue(3).ToString(), rdr.GetValue(4).ToString());
            return pro;
        }

        public override TProductType_PIMInfoInfo GetEntityRecord()
        {
            TProductType_PIMInfoInfo pro = new TProductType_PIMInfoInfo();
            return pro;
        }

        public override string GetInsertsp()
        {
            return "TProductType_PIMInfo_Insert";
        }

        public override string GetUpdatesp()
        {
            return "TProductType_PIMInfo_Update";
        }

        public override string GetDeletesp()
        {
            return null;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_TPRODUCTTYPE_PIM_PRODUCTTYPEID, SqlDbType.NVarChar, 50);
            return Parm;
        }        

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TPRODUCTTYPE_PIM_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TPRODUCTTYPE_PIM_TESTITEMNAME, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TPRODUCTTYPE_PIM_TESTFRE, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TPRODUCTTYPE_PIM_TESTPORT, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TPRODUCTTYPE_PIM_TESTANGLE, SqlDbType.NVarChar, 50),
                };
            return parms;
        }

        public override void SetParameters(TProductType_PIMInfoInfo pvti, params SqlParameter[] Parms)
        {
            Parms[0].Value = pvti.ProductTypeID;
            Parms[1].Value = pvti.TestItemName;
            Parms[2].Value = pvti.TestFre;
            Parms[3].Value = pvti.TestPort;
            Parms[4].Value = pvti.TestAngle;
        }
        #endregion

        public SqlParameter[] GetItemParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TPRODUCTTYPE_PIM_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TPRODUCTTYPE_PIM_TESTITEMNAME, SqlDbType.NVarChar, 50),
                };
            return parms;
        }
    }
}
